/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package AplManager.controller;
import AplManager.views.*;
import AplManager.models.*;
import java.awt.event.ActionEvent;
import java.io.*;
import java.awt.event.ActionListener;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
/**
 *
 * @author user
 */
public class FileIOController {
     private StartingLineUp view;
      //private TableDisplay tableDisplayView;
     private Team model;
   // private ActionListener actionListener;
    
    public FileIOController(Team model){
       this.model = model;
    }
    
    public void control(){
        JFileChooser jFileChooser1 = new javax.swing.JFileChooser();
         File file = new File("players.csv");
        int result = jFileChooser1.showSaveDialog(null);
       if (result == JFileChooser.APPROVE_OPTION){
        file = jFileChooser1.getSelectedFile();
  
     
     try{
       PrintWriter out;
    out = new PrintWriter(file);
    for(int i=0; i<model.getCurSize(); i++){
    String line = model.getPlayers()[i].getFirstName() + "," +
                  model.getPlayers()[i].getlastName()+ "," +
                  model.getPlayers()[i].getNumber() + "," +
                  model.getPlayers()[i].getPosition() + "," +
                  model.getPlayers()[i].getGoals() + "," +
                  model.getPlayers()[i].getAssists();
         out.println(line);           
    }
    out.close(); 
     JOptionPane.showMessageDialog(null,"Saving Succesful");
    }catch(FileNotFoundException e){
   JOptionPane.showMessageDialog(null,"Saving failed!");
   }
}
}
}
